R, Rstudio and GitHub

Tanzanian HCES Analysis and TFNC Training

Liberty Mlambo

Introduction to R, RStudio, Git and GitHub (Week 1)

R and Rstudio

  • R is a free software environment for statistical computing and graphics
  • Rstudio is a integrated development environment (IDE) for R and Python.
    • open source and commercial editions (Windows, Mac, and Linux)

Rstudio (cont..)

  • console for direct code execution

  • syntax-highlighting editor

  • tools for plotting

  • viewing history

  • managing your workspace

Git

Git

Open source

Tracks file changes

Branch based non-linear workflows (we can all work at the same time)

Rstudio IDE intergration

Speed of work (for quick homework assistance)

Local environment

GitHub

GitHub is a web-based interface that uses Git, to let multiple people work and make changes on a project concurrently

Fremium Repository based

  • Repository based (Projects)

Built-in security

File History Tracking

Unlimited Public/ Private Repositories

Track issues and task progress

Track issues and task progress

Practical Demonstration

Practical Assumptions

  • Computer (Mac, Windows, Linux, etc..) with administrative privileges
  • Software download and installation experience

Practical Requirements

  • GitHub account
  • Rstudio
  • Git

Exercises

Exercises 1

Activity Description
1.1 GitHub sign-up GitHub signup
1.2 R download and installation R download
1.3 Rstudio download and installation Rstudio
1.4 Git download and installation Git

Exercises 2 - RStudio and Git connection (optional)

Action Command
In Rstudio console type usethis::create_github_token()
In your github account generate and copy token
In Rstudio console type gitcreds::gitcreds_set()
In Rstudio console paste token
In Rstudio terminal git config user.name "YourGitHubUsername"
In Rstudio terminal git config user.email "YourGitHubEmail@here.com"

Linking GitHub and R (Week 2)

Week 1 Recap

  • Last week we introduced Git, GitHub, R and RStudio.
  • Exercise 1: – Creating GitHub credential – Downloading and Installing R, RStudio and Git
  • Exercise 2 (Optional) - Linking GitHub and R

Configuring Git from RStudio (Exercise 2)

Configure the local Git by telling it who you are. In Rstudio Terminal:

STEP 1 : GitHub account username

First tell Git your GitHub username e.g. dzvoti999

git config --global user "dzvoti99"

STEP 2: GitHub account email address

Second tell Git your GitHub email address e.g. cer*************i@gmail.com

git config --global user.email "cer*************i@gmail.com"

STEP 3: Generate SSH Key pairs in Rstudio

To create access keys for GitHub in RStudio.

  1. Go to Global Options (from the Tools menu)
  2. Click Git/SVN
  3. Check Enable version control interface for RStudio projects

STEP 3: Generate SSH Key pairs in Rstudio…

  1. Click Create SSH Key

STEP 3: Generate SSH Key pairs in Rstudio…

  1. Click View public key
  2. Copy public key

STEP 4: Adding the SSH key to your GitHub account

  1. Go to your GitHub account in the browser
  2. Click your GitHub profile photo(right corner) then Click on settings.

STEP 4: Adding the SSH key to your GitHub account…

  1. Click SSH and GPG keys in the Access on the left side

STEP 4: Adding the SSH key to your GitHub account…

  1. Click New SSH key to add the SSH key we copied from Rstudio

STEP 4: Adding the SSH key to your GitHub account…

  1. Give your key a Title e.g. Rstudio
  2. Leave key type as Authentification Key
  3. Paste the SSH key copied from Rstudio

8. Click Add Key

GitHub workflow

Important GitHub terms

git clone

  • git clone copies/clones your remote repository(GitHub) to your local Rstudio
  • done to link a GitHub repo to Rstudio
  • only done once, when you begin working on a new project

git add

  • The git add command adds new or changed files in your working directory to the Git staging area.
  • This prepares the files for commits

git commit

  • git commitcreates a snapshot of your entire repository at specific times.
  • make new commits after every script change.
  • every commit should be accomanied by a commit message

Commit message

  • Commit messages should be short and descriptive of your change.
  • should tell a story
  • When looking through your repository changes/history you’ll be guided by the commit messages,

Git push

  • git push updates the remote repository on GitHub with local commits.
    • run git push regularly in Rstudio to stay in sync with GitHub

Git pull

  • git pull updates your current local repository branch in Rstudio from the GitHub cloud
  • run git pull regularly in Rstudio to stay in sync with GitHub

GitHub workflow

Create new repository

  • GitHub repositories store a projects e.g. TFNC-Assignments
  • Individual ownership or shared(collaborator access)
  • Repository’s visibility can be public or private

**Section adopted from https://docs.github.com/en/get-started/quickstart/create-a-repo

  1. In the upper-right corner of any page, use the drop-down menu, and select New repository.

  1. Type a short, memorable name for your repository. e.g., TFNC-Assignments
  2. Write a short description
  3. Select private visibility (to make your repo private)

  1. Check `Add a ReadMe file
  2. Select the R gitignore template

  1. Select a licence

  1. Create repo

  1. Repository landing page

Clone GitHub repo to Rstudio

In GitHub

  • On your repository(TFNC-Assignments) page
  • Click code
  • Click SSH
  • Copy the link e.g. git@github.com:dzvoti/TFNC-Assignments.git

In Rstudio

  • Click New Project

  • Select Version Control

  • Select Git

  • Paste the repository url you copies from GitHub e.g.git@github.com:dzvoti/TFNC-Assignments.git`
  • Git your project directory(folder) a name (Best to use same name as repo)
  • Tell git where to place the folder

Rstudio is now synced with GitHub and you can see your files in Rstudio

GitHub workflow

Add files in Rstudio

Add files

Create a new file called First-script.R with the code below

library(tidyverse)

Stage files

  • File will appear in the Git tab

  • Check the box next to a file (stages) adds it to the stagging area

Write commit message

  • Write a suitable commit message and click commit

Commits confirmed

  • Confirm commits and click close

Git status

  • After commits git will check the remote repo and tell you that Your branch is ahead of origin/main by 1 commit

Git push

  • Click the green arrow to push changes back to github
  • Check github and you will see your files there

Exercises

Exercise 1

Clone the dzvoti/TFNC-Training repo to your local Rstudio.

Additional Resources

Additional Resources